package com.erlinyou.im.web.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.erlinyou.im.web.controller.base.JsonUtil;
import com.erlinyou.im.web.controller.base.RequestUtil;

@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("@annotation(com.erlinyou.im.web.aspect.SystemLog)")
//    @Pointcut("execution(public * com.erlinyou.im.web.controller.test.TestController.*(..))")
    public void log() {
    }
    
    
    
//    @Around("searchLog()")
//    public Object process(ProceedingJoinPoint point) throws Throwable {
//    	point.proceed();
//        return null;
//    }
    

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
       
    }

    @After("log()")
    public void doAfter() {
//        logger.info("doAfter");
    }

    @AfterReturning(returning = "object", pointcut = "log()")
    public void doAfterReturning(JoinPoint joinPoint,Object object){
    	try {
    		 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
             HttpServletRequest request = attributes.getRequest();
             logger.info("url:"+request.getRequestURL()+",ip:"+RequestUtil.getRemoteHost(request)
             +",method:"+request.getMethod()+",parames:"+RequestUtil.getRequestMap(request)+",result:"+JsonUtil.ObjectToJson(object));
		} catch (Exception e) {
			logger.error("doAfterReturning error:",e);
		}
    }
    
    
    

}
